package com.tcat.item.web;

import com.tcat.common.vo.PageResult;
import com.tcat.item.pojo.Orders;
import com.tcat.item.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单管理")
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 分页查询订单
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param search
     * @return
     */
    @ApiOperation(value = "分页查询订单")
    @GetMapping("page")
    public ResponseEntity<PageResult<Orders>> queryPermissionsByPage(
            @RequestParam(value="page",defaultValue = "1") Integer page,
            @RequestParam(value="rows",defaultValue = "5") Integer rows,
            @RequestParam(value="sortBy",required = false) String sortBy,
            @RequestParam(value="desc",defaultValue = "false") Boolean desc,
            @RequestParam(value="search",required = false) String  search
    ){
        return ResponseEntity.ok(orderService.queryOrderByPage(page,rows,sortBy,desc,search));
    }


    /**
     * 新增订单
     * @param order
     * @return
     */
    @ApiOperation(value = "新增订单")
    @PostMapping
    public ResponseEntity<Void> saveOrders(Orders order){
        orderService.saveOrders(order);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除tb_rps中的数据，单个删除、多个删除二合一
     * @param oid
     * @return
     */
    @DeleteMapping("oid/{oids}")
    public ResponseEntity<Void> deletePermissions(@PathVariable("oids") String oid){
        String separator="-";
        if(oid.contains(separator)){
            String[] ids=oid.split(separator);
            for(String id:ids){
                this.orderService.deleteOrders(Long.parseLong(id));
            }
        }else{
            this.orderService.deleteOrders(Long.parseLong(oid));
        }
        return ResponseEntity.status(HttpStatus.OK).build();
    }



    /**
     * 订单修改
     * @param order
     * @return
     */
    @ApiOperation(value = "修改订单")
    @PutMapping
    public ResponseEntity<Void> updateOrders(Orders order){
        this.orderService.updateOrders(order);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }



    @GetMapping("/countOrders")
    public int queryOrders(){
        return orderService.countOrders();
    }





}
